package com.weaver.drools.common.drools.executor;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.kie.api.KieBase;
import org.kie.api.event.rule.ObjectDeletedEvent;
import org.kie.api.event.rule.ObjectInsertedEvent;
import org.kie.api.event.rule.ObjectUpdatedEvent;
import org.kie.api.event.rule.RuleRuntimeEventListener;
import org.kie.api.runtime.KieSession;

import com.weaver.drools.common.entity.ActRule;
import com.weaver.drools.common.log.DroolsLog;
import com.weaver.drools.common.utils.KieUtils;

/**
 *  @application name: drools规则执行器
 *  @author: zhouxinlei 
 *  @time：2018年10月8日
 *  @version：ver 1.1
 */
public class RuleExecutor {
	private static KieBase kieBase;
	private static DroolsLog droolsLog;
	
	private static KieSession kieSession;

    /**
     * 	单例(非线程安全,避免影响性能)
     * @return
     */
    public static KieBase getKieBase() {
        if (kieBase == null) {
            kieBase = KieUtils.getKieBase();
        }
        return kieBase;
    }
	/**
	 *	  更新kieBase
	 * @return
	 */
    public static KieBase updateKieBase() {
        //重置kiebase
        kieBase = null;
        return getKieBase();
    }
    public static void execute(String agendaGroup,Object object,Map<String,Object> mapList) {
        kieSession = KieUtils.getKieContainer().newKieSession();
        kieSession.getAgenda().getAgendaGroup(agendaGroup).setFocus();
      //drools规则执行器监听
        kieSession.addEventListener(new RuleRuntimeEventListener() {
			
			@Override
			public void objectUpdated(ObjectUpdatedEvent event) {
				DroolsLog.showDroolsLog1("Object--objectUpdated {}",event.getObject().toString());
			}
			
			@Override
			public void objectInserted(ObjectInsertedEvent event) {
				DroolsLog.showDroolsLog1("Object--objectInserted {}",event.getObject().toString());
			}
			
			@Override
			public void objectDeleted(ObjectDeletedEvent event) {
				DroolsLog.showDroolsLog1("Object--objectDeleted {}",event.getOldObject().toString());
			}
		});
        kieSession.setGlobal("droolsLog",droolsLog);
        ActRule actRule = new ActRule();
        kieSession.setGlobal("actRule",actRule);
        for (Entry<String,Object> map : mapList.entrySet()) {
        	kieSession.setGlobal(map.getKey(),map.getValue());
		}
        kieSession.insert(object);
        int ruleFiredCount = kieSession.fireAllRules();
        //结果
        //QueryResults results = kieSession.getQueryResults("queryList");
        kieSession.dispose();
        DroolsLog.showDroolsLog1("更新后 actRule{}",actRule);
        DroolsLog.showDroolsLog1("执行了 {} 条drools规则",ruleFiredCount);
    }
    
	public static List<ActRule> executeList(List<?> list,String agendaGroup,List<ActRule> droolsRules) {
        kieSession = KieUtils.getKieContainer().newKieSession();
        kieSession.getAgenda().getAgendaGroup(agendaGroup).setFocus();
        //drools规则执行器监听
        kieSession.addEventListener(new RuleRuntimeEventListener() {
			
        	@Override
			public void objectUpdated(ObjectUpdatedEvent event) {
				DroolsLog.showDroolsLog1("Object--objectUpdated {}",event.getObject().toString());
			}
			
			@Override
			public void objectInserted(ObjectInsertedEvent event) {
				DroolsLog.showDroolsLog1("Object--objectInserted {}",event.getObject().toString());
			}
			
			@Override
			public void objectDeleted(ObjectDeletedEvent event) {
				DroolsLog.showDroolsLog1("Object--objectDeleted {}",event.getOldObject().toString());
			}
		});
        
        kieSession.insert(list);
        kieSession.insert(droolsRules);
        int ruleFiredCount = kieSession.fireAllRules();
        kieSession.dispose();
        DroolsLog.showDroolsLog1("执行了 {} 条drools规则",ruleFiredCount);
        return null;
    }
}
